package com.school_list.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school_list.entity.Result;
import com.school_list.entity.TTitle;
import com.school_list.service.ITTitleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小方
 * @since 2023-10-26
 */
@Slf4j
@Api(tags = "用户管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/title")
public class TTitleController {

    private final ITTitleService titleService;

    @ApiOperation("遍历信息接口")
    @GetMapping("/list")
    public Result list(){
        List<TTitle> list = titleService.list();
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增用户")
    public Result save(@RequestBody TTitle title){
//
//        title.setCreateTime(LocalDateTime.now());
//        title.setUpdateTime(LocalDateTime.now());
        titleService.save(title);
        return Result.success();
    }

//    根据前端传递的数据获取列表信息
    @ApiOperation("根据姓名查询信息")
    @GetMapping
    public Result listByTitle(@ApiParam("用户名称") @RequestParam("title") String title){

        log.info("前端获取的数据{}",title);
        LambdaQueryWrapper<TTitle> queryWrapper = new LambdaQueryWrapper<TTitle>()
                .like(TTitle::getTitle,title);
        List<TTitle> list = titleService.list(queryWrapper);
        return Result.success(list);
    }

//    删除
    @ApiOperation("根据id删除信息")
    @DeleteMapping("/{id}")
    public Result deleteById(@ApiParam("用户id") @PathVariable("id") Integer id){
        titleService.removeById(id);
        return Result.success();
    }

//    修改
    @ApiOperation("修改用户信息")
    @PutMapping
    public Result updateById(@ApiParam("修改内容")@RequestBody TTitle title){
        log.info("前端获取到的修改信息为：{}",title);
        titleService.updateById(title);
        return Result.success();
    }


}
